package com.controller;

import com.entity.User;
import com.google.common.collect.Lists;
import com.service.UserService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Administrator on 2018-9-26 0026.
 */
@Controller
@EnableAutoConfiguration
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private DiscoveryClient discoveryClient;


    @RequestMapping("/test")
    public void testUser(){
        List<User> userList = userService.getAllList();
        System.out.println("是否扫描到controller 1. . . {}" + userList.iterator());
    }


    @RequestMapping("/client")
    @ResponseBody
    public List<String> testClient(){
        List<String> stringList = Lists.newArrayList();
        if(discoveryClient == null){
            stringList.add("discoveryClient is null");
        } else {
            List<ServiceInstance> serviceInstanceList = discoveryClient.getInstances("STORES");
            for (ServiceInstance serviceInstance : serviceInstanceList) {
                stringList.add(serviceInstance.getServiceId());
                stringList.add(serviceInstance.getScheme());
                stringList.add(String.valueOf(serviceInstance.getUri()));
            }
        }
        return stringList;
    }




}
